package com.gztms.usermgnt.controller;

import com.gztms.enums.AppCodeEnum;
import com.gztms.exception.SystemException;
import com.gztms.usermgnt.pojo.User;
import com.gztms.usermgnt.service.LoginService;
import com.gztms.utils.ResultUtil;
import com.gztms.utils.ValidateUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * @author Zoey
 * @date 2023/3/8 0:58
 * @Description 登录控制类
 **/
@RestController
@Api("登录")
public class LoginController {

    @Autowired
    private LoginService loginService;

    /**
     * 登录
     *
     * @param user
     * @return
     */
    @PostMapping("/login")
    @ApiOperation(value = "登录", notes = "登录")
    public ResultUtil login(@RequestBody User user) {
        // 登录
        // 处理接收数据
        if (!StringUtils.hasText(user.getUid())) {
            return new ResultUtil(ResultUtil.SERVER_ERROR,"缺少必填信息");
//            throw new SystemException(AppCodeEnum.USER_INFO_NULL);
        }
        return loginService.login(user);
    }

    /**
     * 获取用户个人信息
     *
     * @param token
     * @return
     */
    @GetMapping("/user/info")
    public ResultUtil getUserInfo(@RequestParam("token") String token) {
        return loginService.getUserInfo(token);
    }

    /**
     * 注销
     * @return
     */
    // todo 这里不知道为什么一定要加前缀才不会403
    @PostMapping("/user/logout")
    public ResultUtil logout() {
        return loginService.logout();
    }
}
